package com.children.growth.controller;

import com.children.growth.service.SpeechRecognitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * 
 * @author children
 * @since 1.0.0
 */
@RestController
@RequestMapping("/test")
@CrossOrigin(origins = "*", maxAge = 3600)
public class TestController {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SpeechRecognitionService speechRecognitionService;

    /**
     * 测试密码编码
     */
    @GetMapping("/password")
    public Map<String, Object> testPassword(@RequestParam String password) {
        Map<String, Object> result = new HashMap<>();
        
        // 原始密码
        result.put("originalPassword", password);
        
        // 新生成的哈希值
        String newHash = passwordEncoder.encode(password);
        result.put("newHash", newHash);
        result.put("newHashLength", newHash.length());
        
        // 验证新哈希值是否匹配
        boolean newHashMatches = passwordEncoder.matches(password, newHash);
        result.put("newHashMatches", newHashMatches);
        
        // 数据库中的哈希值
        String dbHash = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVEFDa";
        result.put("dbHash", dbHash);
        result.put("dbHashLength", dbHash.length());
        
        // 验证数据库哈希值是否匹配
        boolean dbHashMatches = passwordEncoder.matches(password, dbHash);
        result.put("dbHashMatches", dbHashMatches);
        
        // 测试不同的哈希值
        String wrongHash1 = "$2a$10$N.zmdr9k7u0CQb376NoUnuTJ8iAt6Z5EHsM8lE9lB0sl7iKTVEFDa";
        boolean wrongHash1Matches = passwordEncoder.matches(password, wrongHash1);
        result.put("wrongHash1", wrongHash1);
        result.put("wrongHash1Matches", wrongHash1Matches);
        
        String wrongHash2 = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lB0sl7iKTVEFDa";
        boolean wrongHash2Matches = passwordEncoder.matches(password, wrongHash2);
        result.put("wrongHash2", wrongHash2);
        result.put("wrongHash2Matches", wrongHash2Matches);
        
        // 详细比较
        result.put("dbHashEqualsNewHash", dbHash.equals(newHash));
        result.put("dbHashEqualsWrongHash1", dbHash.equals(wrongHash1));
        result.put("dbHashEqualsWrongHash2", dbHash.equals(wrongHash2));
        
        // 字符级别的比较
        StringBuilder dbHashChars = new StringBuilder();
        StringBuilder newHashChars = new StringBuilder();
        
        for (int i = 0; i < Math.min(dbHash.length(), newHash.length()); i++) {
            if (dbHash.charAt(i) != newHash.charAt(i)) {
                dbHashChars.append("[").append(i).append("]=").append((int)dbHash.charAt(i)).append("(").append(dbHash.charAt(i)).append(") ");
                newHashChars.append("[").append(i).append("]=").append((int)newHash.charAt(i)).append("(").append(newHash.charAt(i)).append(") ");
            }
        }
        
        result.put("dbHashDifferentChars", dbHashChars.toString());
        result.put("newHashDifferentChars", newHashChars.toString());
        
        // 测试一个已知正确的哈希值
        String correctHash = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVEFDa";
        boolean correctHashMatches = passwordEncoder.matches(password, correctHash);
        result.put("correctHash", correctHash);
        result.put("correctHashMatches", correctHashMatches);
        result.put("dbHashEqualsCorrectHash", dbHash.equals(correctHash));
        
        return result;
    }

    /**
     * 测试语音识别
     */
    @PostMapping("/speech-test")
    public Map<String, Object> testSpeechRecognition(@RequestParam("audioFile") MultipartFile audioFile) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 测试语音识别
            String recognizedText = speechRecognitionService.speechToText(audioFile);
            result.put("success", true);
            result.put("recognizedText", recognizedText);
            result.put("serviceAvailable", speechRecognitionService.isAvailable());
            result.put("fileName", audioFile.getOriginalFilename());
            result.put("fileSize", audioFile.getSize());
            result.put("contentType", audioFile.getContentType());
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
            result.put("serviceAvailable", speechRecognitionService.isAvailable());
        }
        
        return result;
    }
} 